package com.xq.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xq.shop.dao.domain.entity.OrderTrends;
import com.xq.shop.dao.mapper.OrderTrendsMapper;
import com.xq.shop.dto.IntegerDto;
import com.xq.shop.service.IOrderTrendsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.shop.vo.OrderTrendsVo;
import enums.DataStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单动态表 服务实现类
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-30
 */
@Service
public class OrderTrendsServiceImpl extends ServiceImpl<OrderTrendsMapper, OrderTrends> implements IOrderTrendsService {

    @Override
    public List<OrderTrendsVo> queryTrendByOrderId(IntegerDto dto) {
        LambdaQueryWrapper<OrderTrends> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderTrends::getStatus, DataStatus.NORMAL.getCode());
        wrapper.eq(OrderTrends::getOrderId, dto.getValue());
        wrapper.orderByDesc(OrderTrends::getId);
        List<OrderTrends> orderTrendsList = list(wrapper);
        return beanToVo(orderTrendsList);
    }

    @Override
    public void readTrendByOrderIdOfXq(IntegerDto dto) {
        LambdaUpdateWrapper<OrderTrends> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(OrderTrends::getOrderId, dto.getValue());
        wrapper.set(OrderTrends::getUserRead, 2);
        update(wrapper);
    }

    private List<OrderTrendsVo> beanToVo(List<OrderTrends> orderTrendsList) {
        List<OrderTrendsVo> voList = new ArrayList<>();
        orderTrendsList.forEach(m -> {
            OrderTrendsVo vo = new OrderTrendsVo();
            BeanUtils.copyProperties(m, vo);
            voList.add(vo);
        });
        return voList;
    }
}
